<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Set or remove a CTRL event handler</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.sapi-windows-generate-ctrl-event.html">« sapi_windows_generate_ctrl_event</a></li>
      <li style="float: right;"><a href="function.sapi-windows-vt100-support.html">sapi_windows_vt100_support »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.misc.html">杂项 函数</a></li>
    <li>Set or remove a CTRL event handler</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.sapi-windows-set-ctrl-handler" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sapi_windows_set_ctrl_handler</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.4.0, PHP 8)</p><p class="refpurpose"><span class="refname">sapi_windows_set_ctrl_handler</span> &mdash; <span class="dc-title">Set or remove a CTRL event handler</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sapi-windows-set-ctrl-handler-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sapi_windows_set_ctrl_handler</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span><span class="type"></span></span> <code class="parameter">$handler</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$add</code><span class="initializer"> = <strong><code>true</code></strong></span></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Sets or removes a <code class="literal">CTRL</code> event handler, which allows Windows
   CLI processes to intercept or ignore <code class="literal">CTRL+C</code> and
   <code class="literal">CTRL+BREAK</code> events. Note that in multithreaded environments,
   this is only possible when called from the main thread.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.sapi-windows-set-ctrl-handler-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">handler</code></dt>

    <dd>

     <p class="para">
      A callback function to set or remove. If set, this function will be called
      whenever a <code class="literal">CTRL+C</code> or <code class="literal">CTRL+BREAK</code> event
      occurs. The function is supposed to have the following signature:
      <div class="methodsynopsis dc-description">
       <span class="methodname"><strong>handler</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$event</code></span>): <span class="type"><span class="type void">void</span></span></div>

      <dl>
       
        <dt>
<code class="parameter">event</code></dt>

        <dd>

         <span class="simpara">
          The <code class="literal">CTRL</code> event which has been received;
          either <strong><code>PHP_WINDOWS_EVENT_CTRL_C</code></strong>
          or <strong><code>PHP_WINDOWS_EVENT_CTRL_BREAK</code></strong>.
         </span>
        </dd>

       
      </dl>

      Setting a <strong><code>null</code></strong> <code class="parameter">handler</code> causes the process to ignore
      <code class="literal">CTRL+C</code> events, but not <code class="literal">CTRL+BREAK</code> events.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">add</code></dt>

    <dd>

     <p class="para">
      If <strong><code>true</code></strong>, the handler is set. If <strong><code>false</code></strong>, the handler is removed.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sapi-windows-set-ctrl-handler-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sapi-windows-set-ctrl-handler-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="sapi-windows-set-ctrl-handler.example.basic">
   <p><strong>示例 #1 Basic <span class="function"><strong>sapi_windows_set_ctrl_handler()</strong></span> Usage</strong></p>
   <div class="example-contents"><p>
     This example shows how to intercept <code class="literal">CTRL</code> events.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">ctrl_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;$event</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(</span><span style="color: #0000BB">$event</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">PHP_WINDOWS_EVENT_CTRL_C</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"You&nbsp;have&nbsp;pressed&nbsp;CTRL+C\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">PHP_WINDOWS_EVENT_CTRL_BREAK</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"You&nbsp;have&nbsp;pressed&nbsp;CTRL+BREAK\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">sapi_windows_set_ctrl_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">'ctrl_handler'</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">true</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;infinite&nbsp;loop,&nbsp;so&nbsp;the&nbsp;handler&nbsp;can&nbsp;be&nbsp;triggered<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sapi-windows-set-ctrl-handler-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.sapi-windows-generate-ctrl-event.html" class="function" rel="rdfs-seeAlso">sapi_windows_generate_ctrl_event()</a> - Send a CTRL event to another process</span></li>
  </ul>
 </div>


</div></div></div></body></html>